package com.kun.valiant.web.controller.valiant;

import com.kun.valiant.business.service.IBusinessService;
import com.kun.valiant.common.core.controller.BaseController;
import com.kun.valiant.common.core.domain.AjaxResult;
import com.kun.valiant.common.core.page.TableDataInfo;
import com.kun.valiant.contract.domain.Contract;
import com.kun.valiant.contract.domain.vo.ContractApproveInfo;
import com.kun.valiant.contract.domain.vo.ContractSummary;
import com.kun.valiant.contract.service.IContractService;
import com.kun.valiant.system.service.ISysUserService;
import org.flowable.common.engine.impl.util.io.StringStreamSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 *  前端控制器
 * @author fangkun
 * @since 2023-06-19
 */
@RestController
@RequestMapping("/valiant/contract")
public class ContractController extends BaseController {

    @Autowired
    IContractService contractService;

    @Autowired
    ISysUserService sysUserService;

    @Autowired
    IBusinessService businessService;

    @PostAuthorize("hasPermission('valiant:contract:create')")
    @PostMapping("/upload")
    public AjaxResult uploadContractFile(HttpServletRequest req,MultipartFile file){
        return contractService.uploadContractFile(req,file);
    }

    @PostAuthorize("hasPermission('valiant:contract:create')")
    @DeleteMapping("/{year}/{month}/{day}/{name}")
    public AjaxResult deleteContractFile(@PathVariable String year,@PathVariable String month,@PathVariable String day,@PathVariable String name){
        return contractService.deleteContractFile(year,month,day,name);
    }

    @PostAuthorize("hasPermission('valiant:contract:create')")
    @GetMapping("/users/{deptId}")
    public AjaxResult getUsersByDeptId(@PathVariable Long deptId){
        return AjaxResult.success(sysUserService.getUsersByDeptId(deptId));
    }

    /**
     * 添加合同
     * @param contract
     * @return
     */
    @PostAuthorize("hasPermission('valiant:contract:create')")
    @PostMapping
    public AjaxResult addContract(@RequestBody Contract contract){
        return contractService.addContract(contract);
    }

    @PostAuthorize("hasPermission('valiant:contract:create')")
    @GetMapping("/customer/{phone}")
    public AjaxResult getContractInfoByPhone(@PathVariable String phone){
        return contractService.getContractInfoByPhone(phone);
    }

    /**
     *
     * @return
     */
    @PostAuthorize("hasPermission('valiant:contract:list')")
    @GetMapping("/unapprove")
    public TableDataInfo getUnapproveTask(){
        startPage();
        ArrayList<ContractSummary> list = contractService.getUnapproveTask();
        return getDataTable(list);
    }

    @PostAuthorize("hasPermission('valiant:contract:view')")
    @GetMapping("/views/{contractId}")
    public AjaxResult getContractById(@PathVariable Long contractId){
        return contractService.getContractById(contractId);
    }

    @PostAuthorize("hasPermission('valiant:contract:view')")
    @GetMapping("/{year}/{month}/{day}/{name}")
    public AjaxResult showContractPDF(@PathVariable String year, @PathVariable String month, @PathVariable String day, @PathVariable String name){
        return contractService.showContractPDF(year,month,day,name);
    }

    /**
     * 查询当前用户已经提交但是还未审批的任务列表
     * @return
     */
    @PreAuthorize("hasPermission('valiant:contract:list')")
    @GetMapping("/committed_task")
    public TableDataInfo getCommittedTask() {
        startPage();
        List<ContractSummary> list = contractService.getCommittedTask();
        return getDataTable(list);
    }

    @PreAuthorize("hasPermission('valiant:contract:approve')")
    @PostMapping("/approve")
    public AjaxResult approveOrReject(@RequestBody ContractApproveInfo contractApproveInfo) {
        return contractService.approveOrReject(contractApproveInfo);
    }

    /**
     * 返回当前登录用户已经提交的并且审批通过的流程
     * @return
     */
    @PreAuthorize("hasPermission('valiant:contract:list')")
    @GetMapping("/approved")
    public TableDataInfo approvedTask() {
        startPage();
        List<ContractSummary> list = contractService.approvedTask();
        return getDataTable(list);
    }

    @PreAuthorize("hasPermission('valiant:contract:approve')")
    @PutMapping
    public AjaxResult updateContract(@RequestBody Contract contract) {
        return contractService.updateContract(contract);
    }
}












